package org.tbjd.client.decorators;

import java.util.ArrayList;
import java.util.List;

import org.tbjd.client.beans.Task;
import org.tbjd.client.customWidgets.taskList.TreeGridItem;
import org.tbjd.client.customWidgets.taskList.TreeGridItemHandel;
import org.tbjd.client.guis.AbstractItem;
import org.tbjd.client.interfaces.TaskDecorator;

import com.google.gwt.user.client.ui.FocusListener;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class TaskListDecorator implements TaskDecorator {

	private static TaskListDecorator instance = null;

	private TaskListDecorator() {
	}

	public static TaskListDecorator getInstance() {
		if (instance == null) {
			instance = new TaskListDecorator();
		}

		return instance;
	}

	public List<?> decorateAsList(List<Task> lstTasks) {

		ArrayList<TreeGridItem> lstTreeGridItems = new ArrayList<TreeGridItem>();

		for (Task eachTask : lstTasks) {
			TreeGridItem treeGridItem = new TreeGridItem();
			ArrayList<Widget> lstWidgets = new ArrayList<Widget>();

			TreeGridItemHandel handle = new TreeGridItemHandel(eachTask.getName());
			Label lblDaysLeft = new Label(eachTask.getDaysLeft() + "d");
			Label lblProgress = new Label(eachTask.getProgress() + "%");

			if (eachTask.getLstChildren().size() > 0) {
				handle.addStyleName("phase");
				lblDaysLeft.addStyleName("phase");
				lblProgress.addStyleName("phase");
			} else {
				handle.addStyleName("task");
				lblDaysLeft.addStyleName("task");
				lblProgress.addStyleName("task");
			}

			lstWidgets.add(handle);
			lstWidgets.add(lblDaysLeft);
			lstWidgets.add(lblProgress);

			treeGridItem.setValues(lstWidgets);
			treeGridItem.setBean(eachTask);
			treeGridItem.setPosition(eachTask.getLevel());

			if (eachTask.getLstChildren().size() > 0) {
				for (Task child : eachTask.getLstChildren()) {
					TreeGridItem treeGridItemChild = new TreeGridItem();
					ArrayList<Widget> lstChildWidgets = new ArrayList<Widget>();

					TreeGridItemHandel handleChild = new TreeGridItemHandel(child.getName());
					Label lblChildDaysLefte = new Label(child.getDaysLeft() + "d");
					Label lblChildProgress = new Label(child.getProgress() + "%");

					handleChild.addStyleName("subTask");

					lstChildWidgets.add(handleChild);
					lstChildWidgets.add(lblChildDaysLefte);
					lstChildWidgets.add(lblChildProgress);

					treeGridItemChild.setValues(lstChildWidgets);
					treeGridItemChild.setBean(child);
					treeGridItemChild.setPosition(child.getLevel());
					treeGridItemChild.setParent(treeGridItem);

					treeGridItem.addItem(treeGridItemChild);
				}
			}

			lstTreeGridItems.add(treeGridItem);
		}
		return lstTreeGridItems;
	}

	private class Handle extends FocusPanel {

		HorizontalPanel container = null;
		FocusPanel handlePanel = null;
		TextBox textBox = new TextBox();

		public Handle() {
		}

		public Handle(String value) {

			container = new HorizontalPanel();
			handlePanel = new FocusPanel();

			handlePanel.add(new Label("o"));

			handlePanel.addFocusListener(new FocusListener() {

				public void onFocus(Widget sender) {
					System.out.println("Handle.focus() - " + textBox.getText() + " focus");
				}

				public void onLostFocus(Widget sender) {
					System.out.println("Handle.onLostFocus() - " + textBox.getText() + " lost focus");
				}

			});

			container.add(handlePanel);

			textBox.setText(value);
			textBox.setStyleName("customTextBox");
			container.add(textBox);

			container.setBorderWidth(0);

			// initWidget(container);
			this.add(container);

		}

		public FocusPanel getHandel() {
			return handlePanel;
		}

	}

	public AbstractItem decorate(Task task) {
		return null;
	}

}